package svc

import (
	"go-zero-mall/app/admin/cmd/rpc/internal/config"
	"go-zero-mall/app/admin/model"

	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config                         config.Config
	RedisClient                    *redis.Redis
	UmsAdminModel                  model.UmsAdminModel
	UmsMenuModel                   model.UmsMenuModel
	UmsRoleModel                   model.UmsRoleModel
	UmsPermissionModel             model.UmsPermissionModel
	UmsResourceCategoryModel       model.UmsResourceCategoryModel
	UmsResourceModel               model.UmsResourceModel
	UmsRoleMenuRelationModel       model.UmsRoleMenuRelationModel
	UmsRolePermissionRelationModel model.UmsRolePermissionRelationModel
	UmsRoleResourceRelationModel   model.UmsRoleResourceRelationModel
	UmsAdminRoleRelationModel      model.UmsAdminRoleRelationModel
	UmsMemberLevelModel            model.UmsMemberLevelModel
}

func NewServiceContext(c config.Config) *ServiceContext {
	sqlConn := sqlx.NewMysql(c.DB.DataSource)
	return &ServiceContext{
		Config: c,
		RedisClient: redis.New(c.Redis.Host, func(r *redis.Redis) {
			r.Type = c.Redis.Type
			r.Pass = c.Redis.Pass
		}),
		UmsAdminModel:                  model.NewUmsAdminModel(sqlConn, c.Cache),
		UmsMenuModel:                   model.NewUmsMenuModel(sqlConn, c.Cache),
		UmsRoleModel:                   model.NewUmsRoleModel(sqlConn, c.Cache),
		UmsPermissionModel:             model.NewUmsPermissionModel(sqlConn, c.Cache),
		UmsResourceCategoryModel:       model.NewUmsResourceCategoryModel(sqlConn, c.Cache),
		UmsResourceModel:               model.NewUmsResourceModel(sqlConn, c.Cache),
		UmsRoleMenuRelationModel:       model.NewUmsRoleMenuRelationModel(sqlConn, c.Cache),
		UmsRolePermissionRelationModel: model.NewUmsRolePermissionRelationModel(sqlConn, c.Cache),
		UmsRoleResourceRelationModel:   model.NewUmsRoleResourceRelationModel(sqlConn, c.Cache),
		UmsAdminRoleRelationModel:      model.NewUmsAdminRoleRelationModel(sqlConn, c.Cache),
		UmsMemberLevelModel:            model.NewUmsMemberLevelModel(sqlConn, c.Cache),
	}
}
